package com.mojie.overscroll.spring

import android.animation.ValueAnimator
import android.graphics.Canvas
import android.util.Log
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.EdgeEffect
import androidx.core.view.forEach
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * @Author:         lxp
 * @CreateDate:     2024/8/8
 */
class RecyclerViewSpringEdgeEffect : RecyclerView.EdgeEffectFactory() {

    companion object {
        private const val TAG = "RecyclerViewEdgeEffect"
        private const val MAX_SCALE = 0.5f
        private const val FLING_TRANSLATION_TIME = 0.01f//0.01移动的距离
        private const val FLING_BASE_DISTANCE = 300f//基于此数据计算最大缩放
    }

    private var isTop = false
    override fun createEdgeEffect(recyclerView: RecyclerView, direction: Int): EdgeEffect {
        return object : EdgeEffect(recyclerView.context) {
            override fun onPull(deltaDistance: Float) {
                super.onPull(deltaDistance)
                handlePull(deltaDistance)
            }

            /**
             * 滑动
             */
            override fun onPull(deltaDistance: Float, displacement: Float) {
                super.onPull(deltaDistance, displacement)
                handlePull(deltaDistance)
            }

            private fun handlePull(deltaDistance: Float) {
                Log.d(TAG, "handlePull: $direction")
                isTop = direction == DIRECTION_TOP
                val scale = MAX_SCALE * deltaDistance
                if (direction == DIRECTION_TOP) {
                    recyclerView.pivotX = recyclerView.width / 2f
                    recyclerView.pivotY = 0f
                } else {
                    recyclerView.pivotX = recyclerView.width / 2f
                    recyclerView.pivotY = recyclerView.bottom.toFloat()
                }
                recyclerView.scaleY += scale
            }

            /**
             * 松手
             */
            override fun onRelease() {
                //recyclerView会创建上下左右四个edgeEffect,只要不为null，就会回调onRelease，因此此处会多次调用
                super.onRelease()
                Log.d(TAG, "onRelease " + direction)
                val animator = ValueAnimator.ofFloat(recyclerView.scaleY, 1f).setDuration(500)
                animator.interpolator = DecelerateInterpolator(2.0f)
                if (isTop) {
                    recyclerView.pivotX = recyclerView.width / 2f
                    recyclerView.pivotY = 0f
                } else {
                    recyclerView.pivotX = recyclerView.width / 2f
                    recyclerView.pivotY = recyclerView.bottom.toFloat()
                }
                animator.addUpdateListener { valueAnimator ->
                    recyclerView.scaleY = valueAnimator.animatedValue as Float
                }
                animator.start()
            }

            /**
             * 碰撞
             */
            override fun onAbsorb(velocity: Int) {
                super.onAbsorb(velocity)
                Log.d(TAG, "onAbsorb")
                /*val translationVelocity = velocity * FLING_TRANSLATION_TIME
                val scale =
                    Math.min(translationVelocity / FLING_BASE_DISTANCE * MAX_SCALE, MAX_SCALE)
                val animator = ValueAnimator.ofFloat(1f, 1 + scale, 1f).setDuration(500)
                animator.interpolator = LinearInterpolator()
                if (direction == DIRECTION_TOP) {
                    recyclerView.pivotX = recyclerView.width / 2f
                    recyclerView.pivotY = 0f
                } else {
                    recyclerView.pivotX = recyclerView.width / 2f
                    recyclerView.pivotY = recyclerView.bottom.toFloat()
                }
                animator.addUpdateListener { valueAnimator ->
                    recyclerView.scaleY = valueAnimator.animatedValue as Float
                }
                animator.start()*/
            }

            override fun draw(canvas: Canvas?): Boolean {
                // 设置大小之后，就不会有绘画阴影效果
                setSize(0, 0)
                return super.draw(canvas)
            }
        }
    }
}